using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinSSHMenu
{
    public partial class HostConfig : Form
    {
        TreeView hostList = null;
        String _action;

        public String Action
        {
            get { return _action; }
            set { _action = value; }
        }
        
        public HostConfig()
        {
            InitializeComponent();
        }

        private void HostConfig_Load(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            String arg = txtHostname.Text;
            FileUtil util = new FileUtil();
            util.LaunchSSHProcess(arg, txtTitle.Text);
        }

        /// <summary>
        /// Add a new host entry.  If a submenu is selected then this host
        /// will be a child of that menu, otherwise this host will be a child
        /// of the root node.
        /// </summary>
        /// <param name="sender">Event Sender</param>
        /// <param name="e">Event Args</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            hostList.BeginUpdate();
            TreeNode newNode = new TreeNode();
            newNode.Text = txtTitle.Text;
            newNode.Tag = txtHostname.Text;
            newNode.ImageIndex = 5;
            newNode.SelectedImageIndex = 5;
            newNode.EnsureVisible();

            TreeNode selectedNode = hostList.SelectedNode;
            if (null == selectedNode)
            {
                selectedNode = hostList.Nodes[0];
            }
            
            if (Action.Equals(Constants.ACTION_EDIT))
            {
                hostList.SelectedNode.Text = txtTitle.Text;
                hostList.SelectedNode.Tag = txtHostname.Text;
            }
            else if (Action.Equals(Constants.ACTION_COPY))
            {
                hostList.Nodes.Add(newNode);                
            }
            else
            {
                selectedNode.Nodes.Add(newNode);
            }
            HostList.SelectedNode = newNode;
            hostList.EndUpdate();
            this.Close();
        }

        public System.Windows.Forms.TreeView HostList
        {
            get { return this.hostList; }
            set { this.hostList = value; }
        }
    }
}